﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InputBox
{
    public partial class InputBox : Form
    {
        #region Construct
        public InputBox()
        {
            InitializeComponent();
            UpdateUI();
        }
        #endregion

        #region Properties
        private static DialogResult result = DialogResult.Cancel;
        private bool isNum = false;

        public bool IsNum
        {
            get { return isNum; }
        }
        private bool isID = false;

        public bool IsID
        {
            get { return isID; }
        }
        private bool allowNull = false;

        public bool AllowNull
        {
            get { return allowNull; }
            set { allowNull = value; }
        }
        #endregion

        #region i18n
        private string Warn = "";
        private string DNull = "";
        private string OnlyID = "";
        private string OnlyNum = "";
        private void UpdateUI()
        {
            if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == "zh-CN")
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
            else
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

            System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(InputBox));

            btnOK.Text = rm.GetString("btnOK");
            btnCancel.Text = rm.GetString("btnCancel");
            Warn = rm.GetString("StrWarning");
            DNull = rm.GetString("StrDisNull");
            OnlyNum = rm.GetString("StrOnlyNum");
            OnlyID = rm.GetString("StrOnlyID");
        }
        #endregion

        #region Show
        public static DialogResult show(string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = "InputBox:";
            ib.lblInfo.Text = content;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult show(string text, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.lblInfo.Text = content;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult showAllowNull(string text, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.AllowNull = true;
            ib.lblInfo.Text = content;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult showIDInput(string text, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.isID = true;
            ib.lblInfo.Text = content;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult showNumInput(string text, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.isNum = true;
            ib.lblInfo.Text = content;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult showPasswdInput(string text, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.lblInfo.Text = content;
            ib.txtResult.PasswordChar = '*';
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        public static DialogResult showPasswdInput(string text, char passwordChar, string content, out string ret)
        {
            InputBox ib = new InputBox();
            ib.Text = text;
            ib.lblInfo.Text = content;
            ib.txtResult.PasswordChar = passwordChar;
            ib.ShowDialog();
            ret = ib.txtResult.Text;
            return result;
        }
        #endregion

        #region EventBind
        private void btnOK_Click(object sender, EventArgs e)
        {
            result = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = DialogResult.Cancel;
            this.Close();
        }

        private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (isNum == true)
            {
                lblEx.Text = Warn + OnlyNum;
                if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
                {
                    e.Handled = true;
                    lblEx.Visible = true;
                    btnOK.Enabled = false;
                }
                else
                {
                    lblEx.Visible = false;
                    btnOK.Enabled = true;
                }
            }
            else if (isID == true)
            {
                lblEx.Text = Warn + OnlyID;
                if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8 && e.KeyChar != (char)'X')
                {
                    e.Handled = true;
                    lblEx.Visible = true;
                    btnOK.Enabled = false;
                }
                else
                {
                    lblEx.Visible = false;
                    btnOK.Enabled = true;
                }
            }
        }

        private void txtResult_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnOK.PerformClick();
            else if (e.KeyCode == Keys.Escape)
                btnCancel.PerformClick();
        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {
            if (allowNull != true)
            {
                lblEx.Text = Warn + DNull;
                if (txtResult.Text == "")
                {
                    lblEx.Visible = true;
                    btnOK.Enabled = false;
                }
                else
                {
                    lblEx.Visible = false;
                    btnOK.Enabled = true;
                }
            }
        }

        private void InputBox_Load(object sender, EventArgs e)
        {
            if (!allowNull)
                btnOK.Enabled = false;
        }
        #endregion
    }
}
